/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
	"github.com/spf13/cobra"
	"operator/cicd/util"
)

// kubeCmd represents the kube command
var kubeCmd = &cobra.Command{
	Use:   "kube",
	Short: "setup kubernetes config",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	Run: func(cmd *cobra.Command, args []string) {
		flags := cmd.Flags()
		typ, err := flags.GetString("type")
		if err != nil || typ == "" {
			return
		}
		path, err := flags.GetString("file")
		if err != nil {
			return
		}
		kubeconfig := util.NewKubeConfig(util.KubeType(typ), path)
		if kubeconfig == nil {
			return
		}
		config, err := util.LoadPersistentConfigFromFile()
		if err != nil {
			return
		}
		config.KubeConfig = kubeconfig
		err = util.SavePersistentConfigFromFile(config)
		if err != nil {
			return
		}
	},
}

func init() {
	rootCmd.AddCommand(kubeCmd)
	kubeCmd.PersistentFlags().StringP("file", "f", "", "kubernetes config file path")
	kubeCmd.PersistentFlags().StringP("type", "t", "", "kubernetes config type:in_cluster,in_master,in_config")
}
